package baiqitun.stupro.api.controller;

import baiqitun.stupro.common.exception.enums.support.NotFoundExceptionEnum;
import baiqitun.stupro.common.exception.support.NotFoundException;
import baiqitun.stupro.common.model.Result;
import baiqitun.stupro.common.model.TreeModel;
import baiqitun.stupro.common.model.vo.TreeNodeVo;
import baiqitun.stupro.common.util.IdsUtil;
import baiqitun.stupro.common.util.ResultUtil;
import baiqitun.stupro.common.validate.group.SaveGroup;
import baiqitun.stupro.common.validate.group.UpdateGroup;
import baiqitun.stupro.security.service.SysUserService;
import baiqitun.stupro.subject.entity.SysSubject;
import baiqitun.stupro.subject.service.SysSubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author kedaji
 */
@Api(description = "科目（方向）控制器")
@RequestMapping("/subject")
@RestController
public class SubjectController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysSubjectService sysSubjectService;

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation("使用id获取一个指定的科目（方向）")
    @GetMapping("/{id}")
    public Result<SysSubject> getById(@ApiParam("科目id") @PathVariable("id") @NotBlank(message = "科目id不能为空") String id){
        SysSubject subject = sysSubjectService.getById(id);
        if (subject == null){
            throw new NotFoundException(NotFoundExceptionEnum.SUBJECT_NOT_FOUND);
        }
        return ResultUtil.ok(subject);
    }


    @ResponseStatus(HttpStatus.OK)
    @ApiOperation("列出所有顶级科目及其子科目")
    @GetMapping("/trees")
    public Result<List<TreeModel<TreeNodeVo>>> trees(){
        List<TreeModel<TreeNodeVo>> trees = sysSubjectService.getRootTrees();
        return ResultUtil.ok(trees);
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation("保存一条子科目")
    @PostMapping
    public Result<?> saveChildDepart(Authentication authentication,
                                     @ApiParam("部门信息") @Validated(SaveGroup.class) SysSubject subject){
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        subject.setCreateBy(userDetails.getUsername());
        sysSubjectService.save(subject);
        return ResultUtil.ok();
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation("修改科目信息")
    @PutMapping
    public Result<?> updateById(Authentication authentication,
                                @Validated(UpdateGroup.class) SysSubject subject){
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        subject.setUpdateBy(userDetails.getUsername());
        sysSubjectService.updateById(subject);
        return ResultUtil.ok();
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation("修改科目信息")
    @DeleteMapping
    public Result<?> removeById(Authentication authentication,
                                @NotBlank(message = "科目id不能为空") String id){
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        sysSubjectService.removeById(id, userDetails.getUsername());
        return ResultUtil.ok();
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation("修改科目信息")
    @DeleteMapping("/batch")
    public Result<?> removeByIds(Authentication authentication,
                                 @ApiParam("部门id序列，由多个id使用逗号作为分隔符进行连接") @NotBlank(message = "id序列不能为空")  String ids){
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        List<String> idList = IdsUtil.parser(ids);
        sysSubjectService.removeByIds(idList, userDetails.getUsername());
        return ResultUtil.ok();
    }
}
